import { defineStore } from 'pinia'
import { ref } from 'vue'

import { getC1API, getC2API, getC3API } from '@/apis/product/attr/index'

export const useCategoryStore = defineStore('category', () => {
	// 一级/二级/三级分类数据
	const c1Arr = ref<any>([])
	const c2Arr = ref<any>([])
	const c3Arr = ref<any>([])
	// 一级/二级/三级分类的id
	const c1Id = ref('')
	const c2Id = ref('')
	const c3Id = ref('')
	// 是否禁用下拉菜单
	const disabledSelect = ref(0)

	// 获取c1数据
	const getC1Arr = async () => {
		const res: any = await getC1API()
		c1Arr.value = res.data
	}

	// 清除c2c3数据
	const clearC2C3 = () => {
		c2Arr.value = []
		c3Arr.value = []
		c2Id.value = ''
		c3Id.value = ''
	}

	// 获取c2数据
	const getC2Arr = async () => {
		const res: any = await getC2API(c1Id.value)
		c2Arr.value = res.data
	}

	// 清除c3数据
	const clearC3 = () => {
		c3Arr.value = []
		c3Id.value = ''
	}

	// 获取c3数据
	const getC3Arr = async () => {
		const res: any = await getC3API(c1Id.value)
		c3Arr.value = res.data
	}

	// 清空宣布数据
	const clearAllData = () => {
		c1Arr.value = []
		c2Arr.value = []
		c3Arr.value = []
		c1Id.value = ''
		c2Id.value = ''
		c3Id.value = ''
	}

	// 改变禁用下拉菜单
	const changeDisabledSelect = (val: number) => {
		disabledSelect.value = val
	}

	return {
		c1Arr,
		getC1Arr,
		c2Arr,
		getC2Arr,
		c3Arr,
		getC3Arr,
		c1Id,
		clearC2C3,
		c2Id,
		clearC3,
		c3Id,
		clearAllData,
		disabledSelect,
		changeDisabledSelect
	}
})
